package com.party.admin.web.controller.audit;

import com.party.admin.biz.crowdfund.ProjectTransferBizService;
import com.party.admin.web.dto.AjaxResult;
import com.party.admin.web.dto.input.audit.TransferAuditInput;
import com.party.common.paging.Page;
import com.party.core.exception.BusinessException;
import com.party.core.model.audit.TransferAuditOutput;
import com.party.core.model.crowdfund.*;
import com.party.core.model.member.MemberGroup;
import com.party.core.service.crowdfund.*;
import com.party.core.service.member.IMemberGroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;


/**
 * 众筹人员转移审核控制器
 *
 * @author hqh
 *
 */
@Controller
@RequestMapping(value = "/audit/transfer")
public class TransferAuditController {

    @Autowired
    private IMemberGroupService memberGroupService;
    @Autowired
    private ISupportTransferService supportTransferService;
    @Autowired
    private IProjectService projectService;
    @Autowired
    private ICrowdfundEventService crowdfundEventService;
    @Autowired
    private ProjectTransferBizService projectTransferBizService;

    protected static Logger logger = LoggerFactory.getLogger(TransferAuditController.class);

    @RequestMapping("list")
    public ModelAndView list(TransferAuditInput input, Page page) {
        page.setLimit(20);
        ModelAndView mv = new ModelAndView("audit/zcTransferAuditList");
        String themeName=input.getThemeName();
        String partnerId = input.getPartnerId();

        List<TransferAuditOutput> zcTransferAuditList = supportTransferService.getZcTransferAuditList(themeName,partnerId,page);

        List<MemberGroup> partnerList = memberGroupService.getAllPartnerWithName();
        mv.addObject("page", page);
        mv.addObject("zcTransferAuditList", zcTransferAuditList);
        mv.addObject("partnerList", partnerList);
        mv.addObject("input", input);
        return mv;
    }

    @RequestMapping("detail")
    public ModelAndView detail(@RequestParam(value = "eId")String id,Integer type,ProjectWithAuthor projectWithAuthor, Page page) {
        page.setLimit(20);
        ModelAndView mv = new ModelAndView("audit/zcTransferAuditDetail");
        projectWithAuthor.setDistribution(true);
        projectWithAuthor.setEventId(id);
        projectWithAuthor.setBrokerage(1f);
        projectWithAuthor.setAudit(type==0?true:false);

        CrowdfundEvent ceObj =crowdfundEventService.get(id);

        List<ProjectWithAuthor> directPayList= projectService.listTransferWithAuthorPage(projectWithAuthor, page);
        //待审核
        Integer  auditINGTotal = supportTransferService.getACTShiftToPeopleTotal(id,2);
        //已审核
        Integer  auditEDTotal = supportTransferService.getACTShiftToPeopleTotal(id,3);

        mv.addObject("list", directPayList);
        mv.addObject("projectWithAuthor", projectWithAuthor);
        mv.addObject("auditINGTotal", auditINGTotal==null? 0:auditINGTotal);
        mv.addObject("auditEDTotal", auditEDTotal==null? 0:auditEDTotal);
        mv.addObject("ceObj", ceObj);
        mv.addObject("type", type);
        mv.addObject("eId", id);
        mv.addObject("page", page);
        return mv;
    }

    @RequestMapping("verify")
    @ResponseBody
    public AjaxResult verify(String sourceId, String targetId,Integer checkStatus) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            projectTransferBizService.transferAudit(sourceId,targetId,checkStatus);
        } catch (BusinessException be) {
            logger.error("转移支持者异常", be);
            ajaxResult.setSuccess(false);
            ajaxResult.setDescription(be.getMessage());
            return ajaxResult;
        }catch (Exception e){
            logger.error("转移支持者异常", e);
            ajaxResult.setSuccess(false);
            ajaxResult.setDescription("转移异常");
            return ajaxResult;
        }
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }
}
